﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SYH178Utils;

namespace SYH178DAL
{
    public class Product_Info
    {
        public Product_Info()
        { }
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="Introduction">产品简介</param>
        /// <param name="Remark">备注</param>
        /// <returns></returns>
        public int Add(SYH178Model.Product_Info model, string Introduction, string Remark)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Brand", SqlDbType.NVarChar,50),
					new SqlParameter("@Model", SqlDbType.NVarChar,50),
					new SqlParameter("@Number", SqlDbType.Int,4),
					new SqlParameter("@OriginalPrice", SqlDbType.Decimal,9),
					new SqlParameter("@ActivityPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Img", SqlDbType.VarChar,50),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@IndustryId", SqlDbType.Int,4),
					new SqlParameter("@IsRecommend", SqlDbType.Int,4),
					new SqlParameter("@IsOnLine", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@UserId", SqlDbType.Int,4),
                    new SqlParameter("@Introduction", SqlDbType.NVarChar,-1),
                    new SqlParameter("@Remark", SqlDbType.NVarChar,200)
                                        };
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Brand;
            parameters[2].Value = model.Model;
            parameters[3].Value = model.Number;
            parameters[4].Value = model.OriginalPrice;
            parameters[5].Value = model.ActivityPrice;
            parameters[6].Value = model.Img;
            parameters[7].Value = model.CreateTime;
            parameters[8].Value = model.IndustryId;
            parameters[9].Value = model.IsRecommend;
            parameters[10].Value = model.IsOnLine;
            parameters[11].Value = model.Status;
            parameters[12].Value = model.EndTime;
            parameters[13].Value = model.UserId;
            parameters[14].Value = Introduction;
            parameters[15].Value = Remark;

            int obj = Common.SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Product_Info_Add", parameters);

            return obj;

        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH178Model.Product_Info model, string Introduction, string Remark)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Brand", SqlDbType.NVarChar,50),
					new SqlParameter("@Model", SqlDbType.NVarChar,50),
					new SqlParameter("@Number", SqlDbType.Int,4),
					new SqlParameter("@OriginalPrice", SqlDbType.Decimal,9),
					new SqlParameter("@ActivityPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Img", SqlDbType.VarChar,50),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@IndustryId", SqlDbType.Int,4),
					new SqlParameter("@IsRecommend", SqlDbType.Int,4),
					new SqlParameter("@IsOnLine", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4),
                    new SqlParameter("@Introduction", SqlDbType.NVarChar,-1),
                    new SqlParameter("@Remark", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Brand;
            parameters[2].Value = model.Model;
            parameters[3].Value = model.Number;
            parameters[4].Value = model.OriginalPrice;
            parameters[5].Value = model.ActivityPrice;
            parameters[6].Value = model.Img;
            parameters[7].Value = model.CreateTime;
            parameters[8].Value = model.IndustryId;
            parameters[9].Value = model.IsRecommend;
            parameters[10].Value = model.IsOnLine;
            parameters[11].Value = model.Status;
            parameters[12].Value = model.EndTime;
            parameters[13].Value = model.UserId;
            parameters[14].Value = model.Id;
            parameters[15].Value = Introduction;
            parameters[16].Value = Remark;
            int rows = Common.SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Product_Info_Update", parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = Common.SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Product_Info_Delete", parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH178Model.Product_Info GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            SYH178Model.Product_Info model = new SYH178Model.Product_Info();
            DataSet ds = Common.SqlHelper.ExecuteDataset(Config.SYHConnStr, CommandType.StoredProcedure, "Product_Info_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH178Model.Product_Info DataRowToModel(DataRow row)
        {
            SYH178Model.Product_Info model = new SYH178Model.Product_Info();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["Brand"] != null)
                {
                    model.Brand = row["Brand"].ToString();
                }
                if (row["Model"] != null)
                {
                    model.Model = row["Model"].ToString();
                }
                if (row["Number"] != null && row["Number"].ToString() != "")
                {
                    model.Number = int.Parse(row["Number"].ToString());
                }
                if (row["OriginalPrice"] != null && row["OriginalPrice"].ToString() != "")
                {
                    model.OriginalPrice = decimal.Parse(row["OriginalPrice"].ToString());
                }
                if (row["ActivityPrice"] != null && row["ActivityPrice"].ToString() != "")
                {
                    model.ActivityPrice = decimal.Parse(row["ActivityPrice"].ToString());
                }
                if (row["Img"] != null)
                {
                    model.Img = row["Img"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["IndustryId"] != null && row["IndustryId"].ToString() != "")
                {
                    model.IndustryId = int.Parse(row["IndustryId"].ToString());
                }
                if (row["IsRecommend"] != null && row["IsRecommend"].ToString() != "")
                {
                    model.IsRecommend = int.Parse(row["IsRecommend"].ToString());
                }
                if (row["IsOnLine"] != null && row["IsOnLine"].ToString() != "")
                {
                    model.IsOnLine = int.Parse(row["IsOnLine"].ToString());
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["EndTime"] != null && row["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(row["EndTime"].ToString());
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
            }
            return model;
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
